package com.yty.demo._12_io;

import java.io.*;

/**
 * @author: yangtaoyao
 * @time: 2020/1/16 11:05
 * @description:
 */
public class FileIO {
    public static void main(String[] args) {
        /**
         * 文件字节流
         */
        fileInputStreamTest("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\test1.txt");
        fileOutputStreamTest("src/com/yty/demo/_12_io/txt/OutputStreamOut.txt","OutputStreamOut.txt");
        copy("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\OutputStreamOut.txt", "D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\copy1.txt");

        /**
         * 文件字符流
         */
        fileReaderTest("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\test1.txt");
        fileWriterTest("D:\\IdeaProjects\\javaStudy-demo\\src\\com\\yty\\demo\\_12_io\\txt\\fileWriterOut.txt","fileWriterOut.txt");
    }

    public static void fileInputStreamTest(String path){
        try {
            FileInputStream in=new FileInputStream(path);
            byte[] b=new  byte[1024];
            int len=0;
            //in.read(byte b[])  //返回读取的数据的长度,如果读取到最后一个数据，还会向后读一个，返回-1；
            System.out.println("-------fileInputStreamTest-------");
            while ((len=in.read(b))!=-1){
                System.out.println(new String(b,0,len));
                //String(byte bytes[], int offset, int length),参数1是缓冲数据的数组，参数2是从数组的那个位置开始转化字符串，参数3是
                //总共转化几个字节；
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void fileOutputStreamTest(String path,String txt){
        try {
            FileOutputStream out=new FileOutputStream(path);
            String s=txt;
            out.write(s.getBytes());//void write(byte b[])
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copy(String sourcePath,String targetPath){
        try {
            FileInputStream in=new FileInputStream(sourcePath);
            FileOutputStream out=new FileOutputStream(targetPath);
            byte[] b=new byte[1024];
            int len=0;
            while ((len=in.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void fileReaderTest(String path){
        try {
            FileReader fileReader=new FileReader(path);
            char[] c=new char[10];
            int len=0;
            System.out.println("-------fileReaderTest-------");
            while ((len=fileReader.read(c))!=-1){
                System.out.println(new String(c,0,len));
            }
            fileReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void fileWriterTest(String path,String txt){
        try {
            FileWriter fileWriter=new FileWriter(path);
            fileWriter.write(txt);
            fileWriter.flush();
            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
